<template>
  <a-card :bordered="false" class="card-area">
    <div class="table-page-search-wrapper">
      <a-form layout="vertical">
        <a-row :gutter="48">
          <a-col :sm="{span:12}" :lg="{span:12}" :xl="{span:6}" >
            <a-form-item label="科目码">
              <a-input placeholder="请输入..." v-model="queryParam.kmbm"/>
            </a-form-item>
          </a-col>
          <a-col :sm="{span:12}" :lg="{span:12}" :xl="{span:6}" >
            <a-form-item label="科目名称">
              <a-input placeholder="请输入..." v-model="queryParam.kmmc"/>
            </a-form-item>
          </a-col>
          <template v-if="advanced">
          <a-col :sm="{span:12}" :lg="{span:12}" :xl="{span:6}">
            <a-form-item label="科目类型">
              <a-select :allowClear="true" showSearch optionFilterProp="children" v-model="queryParam.kmlxm">
                <a-select-option v-for="i in TYPE0392" :key="i.id" :value="i.code">{{ i.name }}</a-select-option>
              </a-select>
            </a-form-item>
          </a-col>
          <a-col ::sm="{span:12}" :lg="{span:6}">
            <a-form-item label="招生学年">
              <a-select :allowClear="true" showSearch optionFilterProp="children" v-model="queryParam.zsxn" @change="zsndChange">
                <a-select-option v-for="i in TYPE0387" :key="i.id" :value="i.code">{{ i.name }}</a-select-option>
              </a-select>
            </a-form-item>
          </a-col>
          <a-col :sm="{span:12}" :lg="{span:12}" :xl="{span:6}" >
            <a-form-item label="院系">
              <a-select :disabled="selectMenu.yx.length === 1" :allowClear="true" showSearch optionFilterProp="children" v-model="queryParam.dwh" @change="yxChange">
                <a-select-option v-for="i in selectMenu.yx" :key="i.id" :value="i.dwh">{{ i.dwh+'-'+i.dwmc }}</a-select-option>
              </a-select>
            </a-form-item>
          </a-col>
          <a-col :sm="{span:12}" :lg="{span:12}" :xl="{span:6}">
            <a-form-item label="专业">
              <a-select :allowClear="true" showSearch optionFilterProp="children" v-model="queryParam.zyh" @change="zyChange">
                <a-select-option v-for="i in selectMenu.zy" :key="i.id" :value="i.zyh">{{ i.zyh+'-'+i.zymc }}</a-select-option>
              </a-select>
            </a-form-item>
          </a-col>
          <a-col :sm="{span:12}" :lg="{span:12}" :xl="{span:6}">
            <a-form-item label="研究方向">
              <a-select :allowClear="true" showSearch optionFilterProp="children" v-model="queryParam.yjfxm">
                <a-select-option v-for="i in selectMenu.yz" :key="i.id" :value="i.yjfxm">{{ i.yjfxm.substring(i.yjfxm.length-2) +"-"+ i.yjfxmc }}</a-select-option>
              </a-select>
            </a-form-item>
          </a-col>
          </template>
          <a-col :sm="{span:16}" :xl="{span:8}" style="padding-top: 29px;">
            <a-button type="primary" @click="loadData">查询</a-button>
            <a-button type="primary" @click="exportExcel">导出</a-button>
            <a @click="advanced = !advanced" style="margin-left: 8px">
                {{ advanced ? '收起' : '展开' }}
                <a-icon :type="advanced ? 'up' : 'down'"/>
              </a>
          </a-col>
        </a-row>
      </a-form>
    </div>

    <a-table
      ref="table"
      size="default"
      rowKey="key"
      :columns="columns"
      :loading="loading"
      :dataSource="data"
      :alert="options.alert"
      :pagination="pagination"
			@change="handleTableChange"
      :scroll="{ x: 1500 }"
    >


      <span slot="serial" slot-scope="text, record, index">
        {{ index + 1 }}
      </span>
    </a-table>

  </a-card>
</template>

<script>
// import moment from 'moment'

import { STable } from '~'
import { mapState } from 'vuex'

const selectArr = ['TYPE0387',  'TYPE0392']

export default {
  components: {
    STable
  },
  data () {
    return {
      advanced: true,
      loading: false,
      data: [],
      //当前学年
      doctoralAdmissionYear:'',
      // 查询参数
      queryParam: {
        zsxn:''
      },
      postParams: {},
      selectedRowKeys: [],
      selectedRows:'',
      // 表头
      columns: [
        {
          title: '序号',
          scopedSlots: { customRender: 'serial' }
        },
        {
          title: '科目码',
          dataIndex: 'kmbm'
        },
        {
          title: '科目名称',
          dataIndex: 'kmmc'
        },
        {
          title: '科目类型',
          dataIndex: 'kmlxm'
        },
        {
          title: '院系',
          dataIndex: 'dwmc'
        },
        {
          title: '专业',
          dataIndex: 'zymc'
        },
        {
          title: '所属研究方向码',
          dataIndex: 'yjfxm'
        },
        {
          title: '所属研究方向',
          dataIndex: 'yjfxmc'
        },
        {
          title: '导师姓名',
          dataIndex: 'xm'
        }

      ],

      // custom table alert & rowSelection
      options: {
        alert: { show: false },
      },
      testManageAddVisiable: false,
      testManageEditVisiable: false,
      selectMenu: {
        yx: [], // 院系
        zy:[]
      },
      testManageEditData: {},
      pagination:{
        current:1
      }
    }
  },
  computed: {
    ...mapState({
      systemInfo: state => state.app.systemInfo,
      TYPE0387: state => state.app['TYPE0387'], // 年级
      TYPE0392: state => state.app['TYPE0392']
    })
  },
  watch:{
  },
  created () {

    this.initSelectMenu()

    selectArr.forEach(v => {
      this.$store.dispatch('app/setDictionary', v)
    })
  },
  methods: {
    initSelectMenu () {
      let allRequest = [
        this.$api.base.collegeAll()
      ]
      Promise.all(allRequest).then(res => {
        this.selectMenu = {
          yx: res[0].data
        }
        this.queryParam.dwh=res[0].data[0].dwh;
        this.$api.base.majorAll({lsdwh: this.queryParam.dwh}).then(res => {
        this.$set(this.selectMenu, 'zy', res.data)
      })
       this.$api.base.getSchoolYear().then(res=>{
        this.queryParam.zsxn=res.data.doctoralAdmissionYear;
        this.loadData({
          pageSize:10,
          pageNum:1
        })
      })
      })

    },
    // 选择院系
    yxChange (code) {
      this.$set(this.queryParam, 'zyh', '')
      this.$set(this.queryParam, 'yjfxm', '')
      this.$set(this.selectMenu,'zy','')
      this.$set(this.selectMenu,'fx','')
      if(code){
      this.$api.base.majorAll({lsdwh: code}).then(res => {
        this.$set(this.selectMenu, 'zy', res.data)
      })
      }
    },
     // 选择专业
    zyChange (code) {
      this.$set(this.queryParam, 'yjfxm', '')
      this.$set(this.selectMenu,'fx','')
      if(code){
      let params={sszym: code,ssyxm: this.queryParam.dwh,xwccm:3,zsxn:this.queryParam.zsxn}
      this.$api.base.researchAll(params).then(res => {
        this.$set(this.selectMenu, 'yz', res.data)
      })
      }
    },
   // 加载数据
     loadData(parameter = {}) {
       if(!parameter.pageNum){
         this.pagination.current = 1
         parameter.pageNum = 1
       }
      let params = Object.assign(this.queryParam, parameter)
      this.loading = true;
      this.$api.doctorRecruit.examinationList(params).then(res => {
        // console.log(res)
         const pagination = { ...this.pagination
        };
        pagination.total = 0
        if (res.data && res.data.total) {
          pagination.total = res.data.total;
        }
        this.data = []
        this.pagination = pagination;
        if (res.data && res.data.rows.length) {
          this.data = res.data.rows;
        }
        this.loading = false;
      })
    },
		// 分页
		handleTableChange (pagination, filters, sorter) {
      console.log(pagination);
      const pager = { ...this.pagination };
      pager.current = pagination.current;
      this.pagination = pager;
      this.loadData({
        pageSize: 10,
        pageNum: pagination.current
      });
    },
    // 选择招生年度
    zsndChange (code) {

      if(this.queryParam.zyh && code){

       this.$set(this.queryParam, 'yjfxm', '')
       this.$set(this.selectMenu,'fx','')
        let params={sszym: this.queryParam.zyh,ssyxm: this.queryParam.dwh,xwccm:3,zsxn:code}
        this.$api.base.researchAll(params).then(res => {
        this.$set(this.selectMenu, 'fx', res.data)
      })
     }
    },
    // 添加
    handleCreate () {
      this.testManageAddVisiable = true
    },
    // 关闭添加弹层
    handleCreateClose () {
      this.testManageAddVisiable = false
    },
    // 添加成功
    handleCreateSuccess () {
      this.testManageAddVisiable = false
      this.$message.success('添加成功')
      this.loadData({
        pageSize: 10,
        pageNum: this.pagination.current
      })
    },
    // 修改
    handleEdit (key) {
      this.$api.doctorRecruit.examinationId(key).then(res=>{
        this.testManageEditData=res.data
      })
      this.testManageEditVisiable = true
    },
    // 关闭添加弹层
    handleEditClose () {
      this.testManageEditVisiable = false

    },
    // 添加成功
    handleEditSuccess () {
      this.testManageEditVisiable = false
      this.$message.success('修改成功')
         this.loadData({
        pageSize: 10,
        pageNum: this.pagination.current
      })
    },
    onSelectChange (selectedRowKeys,selectedRows) {
      this.selectedRowKeys = selectedRowKeys
      this.selectedRows=selectedRows;
    },
    // 导出
    exportExcel () {
      this.$export('admissions/zsBsrxkskm/export',this.queryParam)
    },
    // 删除
    handleDelete (key) {
       let _this=this;
       this.$confirm({
         title: '确定删除?',
         onOk() {
           _this.$api.doctorRecruit.examinationDelete(key).then(res=>{
        console.log(res.status)
        if (res.status==1) {
          _this.loadData({
					pageSize: 10,
					pageNum: _this.pagination.current
        });
        _this.$message.success(res.msg)
        }else{
          _this.$message.warning(res.msg)
        }
      })
         },
         onCancel() {
           console.log('Cancel');
         },
         class: 'test',
       });

    },
    // 批量删除
    handleBatchDelete () {
      let _this=this;
      this.$confirm({
         title: '确定删除?',
         onOk() {
          if (!_this.selectedRowKeys.length) {
        _this.$message.warning('请选择需要报到的学生')
        return
      }
      let para=[],params
      _this.selectedRows.forEach(item=>{
        para.push(item.id);
      })
      params=para.join(',')
      _this.$api.doctorRecruit.examinationDelete(params).then(res=>{
        console.log(res.status)
        if (res.status==1) {
           _this.selectedRowKeys = [];
          _this.loadData({
					pageSize: 10,
					pageNum: _this.pagination.current
        });
        _this.$message.success(res.msg)

        }else{
          _this.$message.warning(res.msg)
        }
      })
         },
         onCancel() {
           console.log('Cancel');
         },
         class: 'test',
       });

    }
  }
}

</script>
